package com.bkgy.controller.qualityDaily.controller;

import com.bkgy.controller.qualityDaily.entity.pdo.QualityReq;
import com.bkgy.controller.qualityDaily.service.IQualitydailyMaterialRepService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Tag(name = "质量日报-原料质量影响")
@Controller
@RequestMapping("/qualityDailyMaterial")
public class QualitydailyMaterialRepController {

    @Autowired
    private IQualitydailyMaterialRepService service;

    @ResponseBody
    @Operation(summary = "质量日报》三冷区域》原料质量情况饼图")
    @PostMapping("/materialPieRep")
    public ResultData materialPieRep() {
        QualityReq req = new QualityReq();
        return service.materialPieRep(req);
    }

    @ResponseBody
    @Operation(summary = "质量日报》三冷区域》原料质量情况")
    @PostMapping("/materialRep")
    public ResultData materialRep(@RequestBody QualityReq req) {
        return service.materialRep(req);
    }

    @ResponseBody
    @Operation(summary = "【构建原料质量影响表数据】", description = "质量日报》三冷区域》构建原料质量影响yyyyMMdd")
    @PostMapping("/buildmaterialRepData")
    public ResultData<String> buildmaterialRepData(String startDay, String endDay) {
        try {
            Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
            Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);

            while (d1.before(d2)) {
                service.buildMaterialRepJob(d1);
                d1 = DateUtil.getAfterDate(d1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataSuccess(e.getMessage());
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }
}
